<template>
<view class="container">
	<view class="cu-card case" v-if="showCartPanel">
		<view class="cu-item shadow">
			<view class="cu-bar bg-white solid-bottom">
				<view class="action">收货地址</view>
			</view>
			<view class="cu-list menu-avatar">
				<view class="cu-item">
					<view class="content">
						<view><text v-if="addressData.is_default" class="tag">默认</text> {{ addressData.name }}   {{ addressData.hide_phone }} </view>
						<view class="text-gray text-sm flex">
							<view class="text-cut">{{ addressData.area }} {{ addressData.details }}</view>
						</view>
					</view>
					<view class="action">
						<view class="cuIcon-right text-gray" @click="navToAddress()"></view>
					</view>
				</view>
			</view>
			<view class="cu-form-group cashCoin">
				<view class="title">兑换积分</view>
				<input data-key="coin" number focus readonly v-model="jifen" :disabled="true">
			</view>
			<view class="cu-userjifen">
				<view class="title">积分余额</view>
				<text style="margin-left: 20px;font: inherit;color: inherit;">{{userjifen}}</text>
			</view>			
			<view class="padding flex flex-direction margin-top-lg">
				<button :disabled="enableChechout" class="cu-btn bg-red margin-tb-sm lg" @click="checkOut()">确定</button>
			</view>
		</view>
	</view>

			    <view>
			        <payKeyboard v-if="showKeyBoard" title="超脑智算安全键盘" @success="finish" @close="close"></payKeyboard>
			    </view>
</view>
</template>
<script>
	import payKeyboard from '@/components/mi-payKeyboard/mi-payKeyboard.vue'
	export default {
		components: {
			payKeyboard
		},
		data() {
			return {
				loaded: !1,
				loading: !1,
				showTrialOrderKeyBoard: false,
				showKeyBoard: false,
				enableScroll: !0,
				loadMoreStatus: 2,
				showLoadMoreStatus: !1,
				code: "",
				jifen: "",
				userjifen: "0.00",
				usermoney: "0.00",
				enableChechout: !1,
				passInputTips: "",
				addressData: {},
				address_id:0,
				showCartPanel: !1
			}
		},
		onNavigationBarButtonTap(val) {
			uni.navigateTo({
				url: '/pages/goods/orders',
			});	
		},
		onLoad: function (option) {
			this.code = option.code;
			this.helper.checkLogin();
			this.loadData();
		},
		onShow:function(e){
			let pages = getCurrentPages();
			let currPage = pages[pages.length-1];
			if (currPage.$vm.address_id){
				this.address_id = currPage.$vm.address_id;
				this.loadData();
			}
		},
		methods: {
			// 点击[取消] 关闭输入框 的回调
			close(){
				this.showKeyBoard = false,
				this.enableChechout = !1;
			},			
			loadData:function(){
				//获取基础参数
				// uni.showLoading({
				// 	title:"数据加载中",
				// 	mask:true
				// })
				
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['code='+this.code,'address_id='+this.address_id,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"getCartInfo",
					timestamp:timestamp,
					data:{code:this.code,address_id:this.address_id,token:info.token},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					uni.hideLoading();
					
					switch (res.data.data.status){
						case 'noauth':
							uni.showModal({
								title: "提示",
								content: '帐户未实名，现在实名？',
								showCancel:false,
								confirmText: "现在实名",
								success:function(res){
									if(res.confirm){
										uni.navigateTo({
											url: "/pages/user/certification"
										})
									}
								}
							})
							break;
						case 'nopaypwd':
							uni.showModal({
								title: "提示",
								content: '未设置支付密码',
								showCancel:false,
								confirmText: "现在设置",
								success:function(res){
									if(res.confirm){
										uni.navigateTo({
											url: "/pages/user/repasswd?type=pay"
										})
									}
								}
							})
							break;
						case 'noaddress':
							uni.showModal({
								title: "提示",
								content: '未添加收货地址',
								showCancel:false,
								confirmText: "现在添加",
								success:function(res){
									if(res.confirm){
										uni.navigateTo({
											url: "/pages/address/manage?type=add"
										})
									}
								}
							})
							break;
						case "success":
							this.addressData = res.data.data.address,
							this.userjifen = res.data.data.userjifen,
							this.usermoney = res.data.data.usermoney,
							this.jifen = res.data.data.jifen,
							this.showCartPanel = !0,
							this.loaded = !0;
							break;
						default:
						
							break;
					}
				})
			},
			checkOut: function() {
				this.enableChechout = !0;
				this.showKeyBoard = true
			},
			navToAddress: function() {
				uni.navigateTo({
					url: "/pages/address/index?source=1"
				})
			},
			finish: function(e) {
				this.showKeyBoard = false
				if(e.length < 6){
					uni.showToast({
						title:"密码无效",
						icon:"none"
					})
					this.enableChechout = !1
					return;
				}
				if(this.jifen == '' || parseFloat(this.jifen) <= 0){
					uni.showToast({
						title:"兑换积分错误",
						icon:"none"
					})
					this.enableChechout = !1
					return;
				}
				if(parseFloat(this.jifen) > parseFloat(this.userjifen)){
					uni.showToast({
						title:"兑换积分不足",
						icon:"none"
					})
					this.enableChechout = !1
					return;
				}
				this.enableChechout = !1;
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['pwd='+e,'address_id='+this.addressData.id,'code='+this.code,'timestamp='+timestamp,'token='+info.token]);
				this.helper.req({
					api:"createGoodsOrder",
					timestamp:timestamp,
					data:{pwd:e,address_id: this.addressData.id,code:this.code,token:info.token},
					header:{sign:sign},
					method:'POST',
				}).then(res=>{
					let orderid = res.data.data.orderid;
					this.showKeyBoard = false;
					uni.redirectTo({
						url: "/pages/goods/orders"
					})
				});
			},
		}
	}
</script>

<style lang="scss">
	.tag{
		font-size: 20upx;
		color: red;
		margin-right: 10upx;
		background: #fffafb;
		border: 1px solid #ffb4c7;
		border-radius: 4upx;
		padding: 4upx 10upx;
		line-height: 1;
	}
	.cu-list.menu-avatar > .cu-item .content{
		left: 30upx;
	}
.cu-userjifen{
	    background-color: #fff;
	    padding: 0.5px 16px;
	    display: -webkit-box;
	    display: -webkit-flex;
	    display: flex;
	    align-items: center;
	    min-height: 55px;
}
</style>